<?php
/**
 * 毛文科
 */
namespace app\admin\controller;
use app\admin\model\MyAuth;
use app\admin\model\MyChildren;
use app\admin\model\MySubject;
use builder\Builder;
use Qiniu\Auth;
use think\Db;
use think\Request;

class Mycolumn extends Typeone
{
    public function show_list(Request $request)
    {
        $model_id = $request->param('model_id');
        $obj = new MySubject();
        $obj->setModelId($model_id);
        $model = $obj->getModel();
        $field = $obj->getField();
//        echo '<pre>';
//        print_r($field);
//        exit;
        $builder = Builder::table()
            ->setAction('mycolumn/page?model_id='.$model_id)
            ->setModelId($model_id)
            ->setMonitorAction('switch','typeone/change')
            ->setMonitorAction('edit','typeone/change')
            ->setRightHeader();
        foreach ($model['toops'] as $value){
            if($value=='alldel'){
                $builder = $builder->addToolbarDemo('alldel','全部删除',[],'mycolumn/del');
            }
            if($value=='add'){
                $builder = $builder->addToolbarDemo('add','添加',[],'mycolumn/show_add');
            }
            if($value=='back'){
                $builder = $builder->addToolbarDemo('back','返回');
            }
            if($value=='search'){
                $builder = $builder->addToolbarDemo('search','搜索');
            }
        }
        foreach ($field as $value){
            if($value['show']==1){
                if($value['form_type']=='单行文本'||$value['form_type']=='多行文本'||$value['form_type']=='下拉框'||$value['form_type']=='联动下拉框'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name'],'typeone/change');
                }
                if($value['form_type']=='时间'){
                    $builder = $builder->setColumn('input',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='单图片'){
                    $builder = $builder->setColumn('img',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='多图片'){
                    $builder = $builder->setColumn('imgs',$value['field'],$value['field_name']);
                }
                if($value['form_type']=='开关'){
                    if($value['option']){
                        $array = explode(',',$value['option']);
                    }else{
                        $array = ['开启','关闭'];
                    }
                    $builder = $builder->setColumn('switch',$value['field'],$value['field_name'],'typeone/change',null,'center','',$array);
                }
            }
        }
        foreach ($model['right'] as $value){
            if($value=='edit'){
                $builder->setButton($value,'编辑','mycolumn/show_edit');
            }
            if($value=='del'){
                $builder->setButton($value,'删除','mycolumn/del');
            }
            if($value=='add'){
                $builder->setButton($value,'添加','mycolumn/show_add');
            }
            if($value=='detail'){
                $builder->setButton($value,'查看','typeone/detail');
            }
            if($value=='a'){
                $builder->setButton($value,'编辑字段','typeone/show_list');
            }
        }
        return $builder
//            ->setPage()
            ->fetch();
    }
    public function page(Request $request)
    {
        $data = MyChildren::getDatas(getTable('column'));
//        echo '<pre>';
//        print_r($data);
//        exit;
        foreach ($data as  $key=>$value){
            if($value['pid']==0){
                $value['pid'] = '最上级';
            }else{
                $shuju = Db::name(getTable('column'))
                    ->where('id',$value['pid'])
                    ->find();
                if($shuju){
                    $value['pid'] = $shuju['name'];
                }
            }
            if($value['mokuai_id']){
                $shuju = Db::name(getTable('mokuais'))
                    ->where('id',$value['mokuai_id'])
                    ->find();
                if($shuju){
                    $value['mokuai_id'] = $shuju['name'];
                }
            }else{
                $value['mokuai_id'] = '';
            }
            if($value['model_id']!==0){
                $shuju = Db::name(getTable('models'))
                    ->where('id',$value['model_id'])
                    ->find();
                if($shuju){
                    $value['model_id'] = $shuju['name'];
                }else{
                    $value['model_id'] = '';
                }
            }else{
                $value['model_id'] = '';
            }
            $value['create_time'] = date('Y-m-d',$value['create_time']);
            $data[$key] = $value;
        }
        $respos['total'] = count($data);
        $respos['data'] = $data;
        return json_page_response($respos);
    }
    public function show_add(Request $request)
    {
        $data = $request->param();
//        echo '<pre>';
//        print_r($data);
//        exit;
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
//        $model = $obj->getModel();
//
//        if($data['model_id'])
        if($data['id']??null){
            return $obj->getForm('mycolumn/add',null,true,[],$data['id']);
        }
        return $obj->getForm('mycolumn/add',null,true,[]);
    }
    public function add(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(2);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
//        $field = $obj->getField();
        $array = ['增','删','改','查'];
        $newid = Db::name($table)->insertGetId($data);
        foreach ($array as $value){
            Db::name(getTable('jurs'))
                ->insert([
                   'name'=>$data['name'],
                    'descs'=>$value,
                    'status'=>1,
                    'create_time'=>time(),
                    'cid'=>$newid
                ]);
        }
        if($newid){
            MyAuth::add();
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function show_edit(Request $request)
    {
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $field = $obj->getField();
//        echo '<pre>';
//        print_r($field);
//        exit;
//        $model = $obj->getModel();
//
        return $obj->getForm('mycolumn/edit',$data['id']);
    }
    public function edit(Request $request)
    {
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId(2);
        $data = $obj->getData($data);
        $table = $obj->getModel('table');
        if(Db::name($table)->where('id',$data['id'])->update($data)){
            return redirect('index/errors',['code'=>200]);
        }else{
            return redirect('index/errors',['code'=>205]);
        }
    }
    public function del(Request $request){
        $data = $request->param();
        $obj = new MySubject();
        $obj->setModelId($data['model_id']);
        $table = $obj->getModel('table');
        $newdata = Db::name($table)->where('id','in',$data['id'])->select();
        foreach ($newdata as $value){
            if(isset($value['delete'])&&$value['delete']!==1){
                return json_to_respos('有不可删除数据',205);
            }
        }
        Db::name(getTable('jurs'))
            ->where('cid','in',$data['id'])
            ->delete();
        MyAuth::del();
        if(Db::name($table)->where('id','in',$data['id'])->delete()){
            return json_to_respos('删除成功');
        }else{
            return json_to_respos('删除失败',205);
        }
    }
}